//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "NinePointLinearOp.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
#include <gen/QL/Experimental/Finitedifferences/FdmLinearOp.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::CNinePointLinearOp (Cephei::QL::Experimental::Finitedifferences::INinePointLinearOp^ m) : CFdmLinearOp(CNinePointLinearOp::typeid)
{
    CNinePointLinearOp^ _Cm;
    try
    {
#ifdef HANDLE
        _phNinePointLinearOp = NULL;
#endif
        _Cm = safe_cast<CNinePointLinearOp^> (m);
        _Cm->Lock();
        QuantLib::NinePointLinearOp& _m = static_cast<QuantLib::NinePointLinearOp&> (_Cm->GetReference ()); 
        _ppNinePointLinearOp = new boost::shared_ptr<QuantLib::NinePointLinearOp> (new QuantLib::NinePointLinearOp ( _m ));
        SetFdmLinearOp (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppNinePointLinearOp));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cm != nullptr) _Cm->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::CNinePointLinearOp (UInt64 d0, UInt64 d1, Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher) : CFdmLinearOp(CNinePointLinearOp::typeid)
{
    CFdmMesher^ _Cmesher;
    try
    {
#ifdef HANDLE
        _phNinePointLinearOp = NULL;
#endif
        QuantLib::Size _d0 = (QuantLib::Size)ValueHelper::Convert (d0);
        QuantLib::Size _d1 = (QuantLib::Size)ValueHelper::Convert (d1);
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _ppNinePointLinearOp = new boost::shared_ptr<QuantLib::NinePointLinearOp> (new QuantLib::NinePointLinearOp ( _d0,  _d1,  _mesher ));
        SetFdmLinearOp (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppNinePointLinearOp));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::CNinePointLinearOp (boost::shared_ptr<QuantLib::NinePointLinearOp>& childNative, Object^ owner) : CFdmLinearOp(CNinePointLinearOp::typeid)
{
#ifdef HANDLE
	_phNinePointLinearOp = NULL;
#endif
	_ppNinePointLinearOp = &childNative;
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppNinePointLinearOp));
}
Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::CNinePointLinearOp (QuantLib::NinePointLinearOp& childNative, Object^ owner) : CFdmLinearOp(CNinePointLinearOp::typeid)
{
#ifdef HANDLE
	_phNinePointLinearOp = NULL;
#endif
	_ppNinePointLinearOp = new boost::shared_ptr<QuantLib::NinePointLinearOp> (&childNative);
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppNinePointLinearOp));
    _NinePointLinearOpOwner = owner;
    _FdmLinearOpOwner = owner;
}

Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::CNinePointLinearOp (CNinePointLinearOp^ copy) : CFdmLinearOp(CNinePointLinearOp::typeid)
{
#ifdef HANDLE
	_phNinePointLinearOp = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppNinePointLinearOp = new boost::shared_ptr<QuantLib::NinePointLinearOp> (copy->GetShared());
        _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppNinePointLinearOp));
    }
}
Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::CNinePointLinearOp (System::Type^ t) : CFdmLinearOp(CNinePointLinearOp::typeid)
{
#ifdef HANDLE
	_phNinePointLinearOp = NULL;
#endif
	if (!t->IsSubclassOf(CNinePointLinearOp::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::CNinePointLinearOp (QuantLib::Handle<QuantLib::NinePointLinearOp>& childNative, Object^ owner)  : CFdmLinearOp(CNinePointLinearOp::typeid)
{
	_phNinePointLinearOp = &childNative;
	_ppNinePointLinearOp = &static_cast<boost::shared_ptr<QuantLib::NinePointLinearOp>>(childNative.currentLink());
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppNinePointLinearOp));
    _NinePointLinearOpOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::CNinePointLinearOp (QuantLib::Handle<QuantLib::NinePointLinearOp> childNative)  : CFdmLinearOp(CNinePointLinearOp::typeid)
{
	_phNinePointLinearOp = &childNative;
	_ppNinePointLinearOp = &static_cast<boost::shared_ptr<QuantLib::NinePointLinearOp>>(childNative.currentLink());
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppNinePointLinearOp));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::CNinePointLinearOp (QuantLib::NinePointLinearOp childNative)  : CFdmLinearOp(CNinePointLinearOp::typeid)
{
#ifdef HANDLE
	_phNinePointLinearOp = NULL;
#endif
	_ppNinePointLinearOp = new boost::shared_ptr<QuantLib::NinePointLinearOp> (new QuantLib::NinePointLinearOp (childNative));
    _ppFdmLinearOp = new boost::shared_ptr<QuantLib::FdmLinearOp> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOp> (*_ppNinePointLinearOp));
}
#endif

Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::~CNinePointLinearOp ()
{
    if (_ppNinePointLinearOp != NULL)
    {
	    delete _ppNinePointLinearOp;
        _ppNinePointLinearOp = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::!CNinePointLinearOp ()
{
    if (_ppNinePointLinearOp != NULL)
    {
	    delete _ppNinePointLinearOp;
    }
}
QuantLib::NinePointLinearOp& Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::GetReference ()
{
    if (_ppNinePointLinearOp == NULL) throw gcnew NativeNullException ();
	return **_ppNinePointLinearOp;
}
boost::shared_ptr<QuantLib::NinePointLinearOp>& Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::GetShared ()
{
    if (_ppNinePointLinearOp == NULL) throw gcnew NativeNullException ();
	return *_ppNinePointLinearOp;
}
QuantLib::NinePointLinearOp* Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::GetPointer ()
{
    if (_ppNinePointLinearOp == NULL) throw gcnew NativeNullException ();
	return &**_ppNinePointLinearOp;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::NinePointLinearOp>& Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::GetHandle ()
{
	if (_phNinePointLinearOp == NULL)
	{
		_phNinePointLinearOp = new Handle<QuantLib::NinePointLinearOp> (*_ppNinePointLinearOp);
	}
	return *_phNinePointLinearOp;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::HasNative () 
{
	return (_ppNinePointLinearOp != NULL);
}

Cephei::QL::Experimental::Finitedifferences::INinePointLinearOp^ Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp::Swap (Cephei::QL::Experimental::Finitedifferences::INinePointLinearOp^ m)
{
    CNinePointLinearOp^ _Cm;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cm = safe_cast<CNinePointLinearOp^> (m);
        _Cm->Lock();
        QuantLib::NinePointLinearOp& _m = static_cast<QuantLib::NinePointLinearOp&> (_Cm->GetReference ()); 
    	(*_ppNinePointLinearOp)->swap ( _m );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cm != nullptr) _Cm->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::INinePointLinearOp^ Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp_Factory::Create (Cephei::QL::Experimental::Finitedifferences::INinePointLinearOp^ m)
{
    return gcnew CNinePointLinearOp ( m);
}
Cephei::QL::Experimental::Finitedifferences::INinePointLinearOp^ Cephei::QL::Experimental::Finitedifferences::CNinePointLinearOp_Factory::Create (UInt64 d0, UInt64 d1, Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher)
{
    return gcnew CNinePointLinearOp ( d0,  d1,  mesher);
}
